import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getSearchByTypeApi, getSearchSuggestApi, getSearchSingerApi, getSingerHotSongApi } from '@renderer/api'
import { albumType, artistsType, playlistsType, searchSuggestType, songType } from './type'
import { songsType } from '@renderer/store/type'
export const useSearchStore = defineStore('searchStore', () => {
  const activeIndex = ref(0)
  // 搜索建议返回数据
  const searchSuggest = ref({} as searchSuggestType)
  const albums = ref<albumType[] | undefined>([])
  const artists = ref<artistsType[] | undefined>([])
  const playlists = ref<playlistsType[] | undefined>([])
  const songs = ref<songType[] | undefined>([])
  const singerHotSongList = ref([] as any[])
  const videoList = ref([] as any[])
  const ablumList = ref([] as any[])
  const songList = ref([] as any[])
  const lyricList = ref([] as any[])

  // 搜索返回歌曲
  const searchSongs = ref<songsType[]>([])
  // 搜索歌手
  const searchArtists = ref<artistsType[]>([])

  // 获取搜索建议
  const getSearchSuggest = async (keyword: string) => {
    let res = await getSearchSuggestApi(keyword)
    searchSuggest.value = res.result
    albums.value = searchSuggest.value.albums
    artists.value = searchSuggest.value.artists
    playlists.value = searchSuggest.value.playlists
    songs.value = searchSuggest.value.songs
  }
  // 获取搜索结果
  const getSearch = async (keyword: string, type: number) => {
    let res: any = await getSearchByTypeApi({ keywords: keyword, type })
    switch (type) {
      case 1:
        // 单曲
        searchSongs.value = res.result.songs
        break;
      case 1014:
        // 视频
        videoList.value = res.result.videos
        break;
      case 10:
        // 专辑
        ablumList.value = res.result.albums
        break;
      case 1000:
        // 歌单
        songList.value = res.result.playlists
        break;
      case 1006:
        // 歌词
        lyricList.value = res.result.songs
        break;
    }
  }
  // 搜索歌手
  const getSearchArtists = async (keyword: string) => {
    const res = await getSearchSingerApi(keyword)
    if (res.code === 200) {
      searchArtists.value = res.data.list
    }
  }
  // 点击歌手自动播放歌手热门歌曲
  const getSingerHotSong = async (id: number) => {
    const res = await getSingerHotSongApi(id)
    if (res.code === 200) {
      singerHotSongList.value = res.songs
    }
  }

  return {
    activeIndex,
    albums,
    artists,
    playlists,
    searchArtists,
    songs,
    searchSongs,
    singerHotSongList,
    videoList,
    ablumList,
    songList,
    lyricList,
    getSearchSuggest,
    getSearch,
    getSearchArtists,
    getSingerHotSong
  }
})
